﻿using App.Chat.Contract;

namespace App.Chat.Hubs
{
    public class RemoteEventTrigger : IRemoteEventTrigger
    {
        private readonly IChatHub _chatHub;
        public RemoteEventTrigger(IChatHub chatHub)
        {
            _chatHub = chatHub;
        }

        //未读消息数量提醒更新事件，如好友添加成功，好友添加请求
        public async Task FriendAddAskCountUpdateEventAsync(string userId)
        {
            await SendUpdateEvent(userId, async connId =>
            await _chatHub.SendMsgToSpecialClient(connId, CommonConst.UnReadCountUpdateTipEvent));
        }
        public async Task FriendAddSuccessCountUpdateEventAsync(string userId)
        {
            await SendUpdateEvent(userId, async connId =>
            await _chatHub.SendMsgToSpecialClient(connId, CommonConst.UnReadCountUpdateTipEvent));
        }
        /// <summary>
        /// 更新会话列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task UpdateSessionListEventAsync(string userId)
        {
            await SendUpdateEvent(userId, async connId =>
            await _chatHub.SendMsgToSpecialClient(connId, CommonConst.UpdateSessionListEvent));
        }

        private async Task SendUpdateEvent(string userId, Action<string> sendEvent)
        {
            var connId = await _chatHub.GetConnIdByUserIdAsync(userId);
            if (connId != null)
                sendEvent(connId);
        }
    }
}
